package com.neuedu.yyzx.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.yyzx.entity.Upload;
import com.neuedu.yyzx.framework.Res;
import com.neuedu.yyzx.service.IUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author keyu
 * @since 2024-05-06
 */
@Controller
@RequestMapping("/upload")
public class UploadController {
    @Autowired
    IUploadService uploadService;

    /**
     * http://localhost:8080/upload/list
     * @param upload
     * @return
     */
    @RequestMapping("/list")
    public Res<List<Upload>> list(Upload upload) {

        QueryWrapper<Upload> queryWrapper = new QueryWrapper<>();
        return Res.ok(uploadService.list(queryWrapper));
    }
//    @RequestMapping("/selectOne")
//    public  Res selectOne(String where, Object... params){
//        return Res.ok(uploadService.selectOne(where,params));
//    }
//    @RequestMapping("/insert")
//    public Res insert(@RequestBody Upload upload){
//        return Res.ok(uploadService.saveOrUpdate(upload));
//    }
    //分页查询
    @RequestMapping("/page")
    public Res<Page> page(Page<Upload> page, Upload upload) {
        QueryWrapper<Upload> queryWrapper = new QueryWrapper<>();
        if(upload.getOriginName()!= null && !"".equals(upload.getOriginName().trim())){
            queryWrapper.like("originName" ,upload.getOriginName() );
        }
        return Res.ok(uploadService.page(page,queryWrapper));
    }

    @RequestMapping("/saveOrUpdate")
    public Res  saveOrUpdate( @RequestBody Upload upload){
        return  Res.ok(uploadService.saveOrUpdate(upload));
    }


    @RequestMapping("/removeById/{id}")
    public Res  removeById(@PathVariable int id){
        return  Res.ok(uploadService.removeById(id));
    }


    @RequestMapping("/getById/{id}")
    public Res<Upload> getById(@PathVariable  int id){
        return  Res.ok(uploadService.getById(id));
    }
    
    @RequestMapping("/upload")
    public Res  upload(HttpServletRequest request, HttpServletResponse response) {
        return Res.ok(uploadService.upload(request));
    }

    @RequestMapping("/download")
    public Res  download(HttpServletRequest request, HttpServletResponse response) throws IOException {
        return Res.ok(uploadService.download(request,response));
    }

}
